﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Practice
{
    public partial class Main : Form
    {
        string stringToPrint;

        public Main()
        {
            InitializeComponent();
        }

        // Read all content from selected text file
        private void readEvent(object sender, EventArgs e)
        {
            OpenFileDialog readDlg = new OpenFileDialog();
            readDlg.InitialDirectory = @"C:\Documents and Settings\tung.le\Desktop\";
            readDlg.Filter = "txt files (*.txt)| *.txt" +
                               "|dll files (*.dll)| *.dll" +
                               "|exe files (*.exe)| *.exe" +
                               "|All files (*.*)| *.*";
            readDlg.RestoreDirectory = true;
            readDlg.CheckPathExists = true;
            readDlg.CheckFileExists = true;
            readDlg.Title = "Select a file to read from";

            if (readDlg.ShowDialog() == DialogResult.OK)
                outputBox.Text = System.IO.File.ReadAllText(readDlg.FileName);

            stringToPrint = outputBox.Text;
        }

        // Save all of content retrieved after reading 
        private void saveEvent(object sender, EventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.InitialDirectory = @"C:\Documents and Settings\tung.le\Desktop";
            saveDlg.Filter = "txt files (*.txt)| *.txt" +
                             "|All files (*.*)| *.*";
            saveDlg.Title = "Save File";
            saveDlg.CheckPathExists = true;
            saveDlg.RestoreDirectory = true;

            Stream myStream;

            if (saveDlg.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveDlg.OpenFile()) != null)
                {

                    StreamWriter wText = new StreamWriter(myStream);
                    wText.Write(outputBox.Text);
                    wText.Flush();
                    wText.Close();
                    myStream.Close();
                }
            }
        }

        // Execute "Open" command on the selected file
        private void openEvent(object sender, EventArgs e)
        {
            // Select file
            OpenFileDialog browseDlg = new OpenFileDialog();
            browseDlg.InitialDirectory = @"C:\Documents and Settings\tung.le\Desktop";
            browseDlg.Filter = "txt files (*.txt)| *.txt" +
                               "|dll files (*.dll)| *.dll" +
                               "|exe files (*.exe)| *.exe" +
                               "|All files (*.*)| *.*";
            browseDlg.RestoreDirectory = true;
            browseDlg.CheckPathExists = true;
            browseDlg.CheckFileExists = true;
            browseDlg.Title = "Select a file to open";

            // Execute or Open file
            if (browseDlg.ShowDialog() == DialogResult.OK)
                System.Diagnostics.Process.Start(@browseDlg.FileName);
        }

        // Retrieve text file from link and save it
        private void linkBtn_Click(object sender, EventArgs e)
        {
            // Read link from textbox and load the text document
            string filePath = linkBox.Text;
            if (filePath.Substring(filePath.Length - 3, 3) == "xml")
            {
                XElement file = XElement.Load(@filePath);
                outputBox.Text = file.ToString();
            }
            else if (filePath.Substring(filePath.Length - 3, 3) == "txt")
            {
                System.Net.WebClient client = new System.Net.WebClient();
                outputBox.Text = client.DownloadString(@filePath);
            }
            else
            {
                MessageBox.Show("Error: invalid link or file format.");
            }
            stringToPrint = outputBox.Text;

            // Save file to selected location
            //SaveFileDialog saveDlg = new SaveFileDialog();
            //saveDlg.InitialDirectory = @"C:\Documents and Settings\tung.le\Desktop";
            //saveDlg.Filter = "txt files (*.txt)| *.txt" +
            //                 "|XML files (*.xml)| *.xml" +
            //                 "|All files (*.*)| *.*";
            //saveDlg.Title = "Save File";
            //saveDlg.CheckPathExists = true;
            //saveDlg.RestoreDirectory = true;

            //Stream myStream;

            //if (saveDlg.ShowDialog() == DialogResult.OK)
            //{
            //    if ((myStream = saveDlg.OpenFile()) != null)
            //    {
            //        StreamWriter wText = new StreamWriter(myStream);
            //        wText.Write(file.ToString());
            //        wText.Flush();
            //        wText.Close();
            //        myStream.Close();
            //    }
            //}
        }

        private void aboutMenuItem_Click(object sender, EventArgs e)
        {
            HelpForm helpform = new HelpForm();
            helpform.ShowDialog();
        }

        private void exitEvent(object Sender, EventArgs data)
        {
            Application.Exit();
        }

        // Associate control to Print document
        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            // Set and reset every time
            int charactersOnPage = 0;
            int linesPerPage = 0;

            // Sets the value of charactersOnPage to the number of characters 
            // of stringToPrint that will fit within the bounds of the page.
            e.Graphics.MeasureString(stringToPrint, this.Font, e.MarginBounds.Size,
                StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage);

            // Draws the string within the bounds of the page.
            e.Graphics.DrawString(stringToPrint, outputBox.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);
            e.Graphics.PageUnit = GraphicsUnit.Inch;

            // Remove the portion of the string that has been printed.
            stringToPrint = stringToPrint.Substring(charactersOnPage);

            // Check to see if more pages are to be printed.
            e.HasMorePages = (stringToPrint.Length > 0);

            // If there are no more pages, reset the string to be printed.
            if (!e.HasMorePages)
                stringToPrint = outputBox.Text;
        }

        private void printPreviewMenuItem_Click(object sender, EventArgs e)
        {
            printPreviewDialog.ShowDialog();
        }

        private void printMenuItem_Click(object sender, EventArgs e)
        {
            if (printDialog.ShowDialog() == DialogResult.OK)
                printDocument.Print();
        }

        // Multiple subscription handled in MainForm.Design.cs
    }
}